package com.api.service.impl;

import com.api.model.dto.interfacceInfo.InterfaceInfoAddDTO;
import com.api.model.dto.interfacceInfo.InterfaceInfoQueryDTO;
import com.api.model.dto.interfacceInfo.InterfaceInfoUpdateDTO;
import com.api.model.dto.interfacceInfo.VerifyInterfaceInfoDTO;
import com.api.model.reslut.ResultPage;
import com.api.model.vo.InterfaceInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.api.model.entity.InterfaceInfo;
import com.api.service.InterfaceInfoService;
import com.api.mapper.InterfaceInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author haoshuai
* @description 针对表【interface_info(接口信息)】的数据库操作Service实现
* @createDate 2024-04-26 19:47:13
*/
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService{



    @Override
    public ResultPage<InterfaceInfoVo> getInterfaceInfoPage(InterfaceInfoQueryDTO interfaceInfoQueryDTO) {
        Integer current = interfaceInfoQueryDTO.getCurrent();
        Integer size = interfaceInfoQueryDTO.getPageSize();
        IPage<InterfaceInfo> page = new Page<>(current,size);
        baseMapper.selectPage(page,getQueryWrapper(interfaceInfoQueryDTO));
        List<InterfaceInfo> records = page.getRecords();

        List<InterfaceInfoVo> interfaceInfoVos = new ArrayList<>();
        for (InterfaceInfo record : records) {
            InterfaceInfoVo interfaceInfoVo = new InterfaceInfoVo();
            BeanUtils.copyProperties(record,interfaceInfoVo);
            interfaceInfoVos.add(interfaceInfoVo);
        }

        long pages = page.getPages();
        long size1 = page.getSize();
        long total = page.getTotal();
        long current1 = page.getCurrent();
        return new ResultPage<InterfaceInfoVo>(interfaceInfoVos,total,current1,pages);
    }

    @Override
    public InterfaceInfoVo getInterfaceById(Integer id) {
        InterfaceInfo interfaceInfo = baseMapper.selectById(id);
        InterfaceInfoVo interfaceInfoVo = new InterfaceInfoVo();
        if (interfaceInfo != null){
            BeanUtils.copyProperties(interfaceInfo,interfaceInfoVo);
        }
        return interfaceInfoVo;
    }

    @Override
    public Boolean addInterfaceInfo(InterfaceInfoAddDTO interfaceInfoAddDTO) {

        InterfaceInfo interfaceInfo = new InterfaceInfo();
        BeanUtils.copyProperties(interfaceInfoAddDTO,interfaceInfo);
        return save(interfaceInfo);
    }

    @Override
    public Boolean updateInterfaceInfo(InterfaceInfoUpdateDTO interfaceInfoUpdateDTO) {
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        BeanUtils.copyProperties(interfaceInfoUpdateDTO,interfaceInfo);
        return updateById(interfaceInfo);
    }

    @Override
    public Boolean deleteInterfaceInfo(Integer id) {
        return removeById(id);
    }

    @Override
    public Boolean verifyInterfaceInfo(VerifyInterfaceInfoDTO verifyInterfaceInfoDTO) {
        InterfaceInfo interfaceInfo = getById(verifyInterfaceInfoDTO.getId());
        if (Objects.equals(interfaceInfo.getVerify(), verifyInterfaceInfoDTO.getOldVerify())){
            InterfaceInfo interfaceInfo1 = new InterfaceInfo();
            interfaceInfo1.setId(verifyInterfaceInfoDTO.getId());
            interfaceInfo1.setVerify(verifyInterfaceInfoDTO.getVerify());
            //审核通过默认将接口访问功能打开
            if (verifyInterfaceInfoDTO.getVerify() ==1){
                interfaceInfo1.setStatus(1);
            }
            return updateById(interfaceInfo1);
        }else {
            throw new RuntimeException("审核状态错误,请稍后再试");
        }
    }


    private LambdaQueryWrapper<InterfaceInfo> getQueryWrapper(InterfaceInfoQueryDTO interfaceInfoQueryDTO){
        LambdaQueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<InterfaceInfo>().lambda();
        queryWrapper.eq(interfaceInfoQueryDTO.getVerify()!=null,InterfaceInfo::getVerify,interfaceInfoQueryDTO.getVerify());
        queryWrapper.eq(interfaceInfoQueryDTO.getStatus()!=null,InterfaceInfo::getStatus,interfaceInfoQueryDTO.getStatus());
        return queryWrapper;
    }
}




